/**
 * @author cqb
 * @class OpenLayers.Control.CurtainControl
 * @version 1.0
 * @requires OpenLayers/Control.js
 */
OpenLayers.Control.CurtainControl = OpenLayers.Class(OpenLayers.Control,{
	title: "拉帘",
	/** 
	 * 类型
     * @property type
     * @type {String}
     */
    type: OpenLayers.Control.TYPE_TOOL,
	/**
	 * 拉帘类型竖直或则水平
     * @property align
     * @type {String}
	 */
    align: "virtical",
    /**
     * 进行拉帘的图层对象
     * @property layer
     * @type {Object}
     */
    layer: null,
    /**
     * 进行拉帘的帮助元素
     * @property helper
     * @type {DOM}
     */
    helper: null,
    /**
     * 拉帘的容器
     * @property curtaindiv
     * @type {DOM}
     */
    curtaindiv: null,
    /**
     * 图层的容器
     * @property layercontainer
     * @type {DOM}
     */
    layercontainer: null,
    /**
     * 拉帘四周的空隙
     * @property tolerence
     * @type {Number}
     */
    tolerence: 20,
    
    /**
     * 初始化
	 * @constructor
	 * @param {Object} options 参数
	 */
	initialize : function(options) {
		OpenLayers.Control.prototype.initialize.apply(this,arguments);
		
		var helper = document.createElement("div");
		helper.className = 'curtain_helper';
		helper.style.position = "absolute";
		helper.style.zIndex = "1001";
		this.helper = helper;
	},
	
	/**
	 * 激活
	 * @method activate
	 */
	activate: function(){
		OpenLayers.Control.prototype.activate.apply(this,arguments);
		this.map.viewPortDiv.appendChild(this.curtaindiv);
		
		if(!this.layer){
			return false;
		}
		this.layer.div.style.visibility = "hidden";
		this._appendLayerDiv();
		
		var controls = this.map.getControlsByClass("OpenLayers.Control.Navigation");
		if(controls){
			var control = controls[0];
			control.disableZoomWheel();
		}
		var zoomcontrol = this.map.getControlsByClass("OpenLayers.Control.Zoom");
		if(zoomcontrol){
			var control = zoomcontrol[0];
			if (control.map) {
				control.curtainactive = true;
				control.map.events.unregister("buttonclick", control, control.onZoomClick);
			}
		}
		
		this.events.triggerEvent("curtainActivate");
	},
	
	_appendLayerDiv: function(){
		var layer = this.layer.div.cloneNode(true);
		layer.style.display = "";
		layer.style.visibility = "inherit";
		var imgs = layer.childNodes;
		for(var i=0; i<imgs.length;i++){
			imgs[i].style.visibility = "inherit";
		}
		var c = this.map.layerContainerDiv;
		this.layercontainer.style.left = c.style.left;
		this.layercontainer.style.top = c.style.top;
		this.layercontainer.innerHTML="";
		this.layercontainer.appendChild(layer);
	},
	
	/**
	 * 取消激活
	 * @method deactivate
	 */
	deactivate: function(){
		OpenLayers.Control.prototype.deactivate.apply(this,arguments);
		
		this.layercontainer ? this.layercontainer.innerHTML = "" : false;
		if(this.curtaindiv){
			if(this.curtaindiv.parentNode && OpenLayers.Util.isElement(this.curtaindiv.parentNode)){
				this.map.viewPortDiv.removeChild(this.curtaindiv)
			}
		}
		
		var controls = this.map.getControlsByClass("OpenLayers.Control.Navigation");
		if(controls){
			var control = controls[0];
			control.enableZoomWheel();
		}
		var zoomcontrol = this.map.getControlsByClass("OpenLayers.Control.Zoom");
		if(zoomcontrol){
			var control = zoomcontrol[0];
			if (control.map && control.curtainactive) {
				control.curtainactive = false;
				control.map.events.register("buttonclick", control, control.onZoomClick);
			}
		}
		
		this.events.triggerEvent("curtainDeactivate");
		if(!this.layer){
			return false;
		}
		this.layer.div.style.visibility = "visible";
	},
	
	setLayer: function(layer){
		if(layer){
			if(this.layer){
				this.layer.div.style.visibility = "visible";
				this.deactivate();
			}
			this.layer = layer;
			if(this.active){
				this.layer.div.style.visibility = "hidden";
				this._appendLayerDiv();
			}
		}else{
			if(this.layer){
				this.layer.div.style.visibility = "visible";
			}
			//this.layer = layer;
			this.deactivate();
		}
	},
	
    /**
     * 绘制
     * @method draw
     * @private
     */
    draw: function() {
        this.handler = new OpenLayers.Handler.Drag(this, {
                "move": this.drag
            }, {
                interval: 1,
                documentDrag: false
            }
        );
        
        var size = this.map.getSize();
        var curtaindiv = document.createElement("div");
        curtaindiv.className = 'curtain';
        curtaindiv.style.position = 'relative';
        curtaindiv.style.width = size.w+"px";
        curtaindiv.style.height = size.h+"px";
        curtaindiv.style.overflow = 'hidden';
        curtaindiv.style.zIndex = "1000";
        
        this.curtaindiv = curtaindiv;
        
		if(this.align === "virtical"){
			this.helper.style.height = size.h+"px";
			this.helper.style.width = "1px";
			this.helper.style.top = "0px";
			this.helper.style.left = size.w - this.tolerence+"px";
			this.helper.style.borderRight = "5px solid #333";
			
			this.curtaindiv.style.width = size.w-this.tolerence+5 +"px";
		}else{
			this.helper.style.width = size.w+"px";
			this.helper.style.height = "1px";
			this.helper.style.left = "0px";
			this.helper.style.borderBottom = "5px solid #333";
			this.helper.style.top = size.h-this.tolerence+"px";
			
			this.curtaindiv.style.height = size.h-this.tolerence+5 +"px";
		}
		
		this.curtaindiv.appendChild(this.helper);
		
		var layercontainer = document.createElement("div");
		layercontainer.style.position = "absolute";
		layercontainer.style.width = "100px";
		layercontainer.style.height = "100px";
		
		this.layercontainer = layercontainer;
		this.curtaindiv.appendChild(layercontainer);
		
		this.map.viewPortDiv.appendChild(this.curtaindiv);
    },
    
    /**
     * 拖拽处理函数
     * @method drag
     * @param {Pixel} xy
     */
    drag: function(xy){
    	var size = this.map.getSize();
    	if(this.align === "virtical"){
    		if(xy.x >= this.tolerence && xy.x <= size.w - this.tolerence){
	    		this.helper.style.left = xy.x+"px";
	    		this.curtaindiv.style.width = xy.x+5+"px";
    		}
    	}else{
    		if(xy.y >= this.tolerence && xy.y <= size.h - this.tolerence){
    			this.helper.style.top = xy.y+"px";
    			this.curtaindiv.style.height = xy.y + 5+"px";
    		}
    	}
    },
    
    /**
     * 类名称
     * @property CLASS_NAME
     * @type {String}
     */
    CLASS_NAME: "OpenLayers.Control.CurtainControl"
});